<?php
class Prof_QcmController extends Zend_Controller_Action{
	private $_auth;
	public function init(){
		$this->_auth = Zend_Auth::getInstance();
	}
	public function indexAction(){
		$qcm = new Application_Model_DbTable_Qcm();
		$page =  (int) $this->_getParam('page', 1);
		$paginator = Zend_Paginator::factory($qcm->fetchAllByAuthorWithQuestions($this->_auth->getIdentity()->id));
		$paginator	->setItemCountPerPage(10)
		->setCurrentPageNumber($page)
		->setPageRange(3);
		$this->view->entries = $paginator;
	}
	public function addAction(){
		$request = $this->getRequest();
		$form = new Prof_Form_Qcm();
		if($this->getRequest()->isPost()){
			if($form->isValid($request->getPost())){
				$values = $form->getValues();
				$db = new Application_Model_DbTable_Qcm();
				$values['auteur'] = $this->_auth->getIdentity()->id;
				unset($values['id']);
				$db->insert($values);
				$this->_helper->redirector('index');
			}else{
				$this->view->form = $form;
				$errors = $form->getMessages();
				$messages = array();
				foreach ($errors as $err) {
					foreach($err as $mess){
						$messages[]=$mess;
					}
				}
				$this->view->errors = $messages;
			}
		}else{
			$this->view->form = $form;
		}
	}
	public function editAction(){
		$request = $this->getRequest();
		$form = new Prof_Form_Qcm();
		$id = (int) $this->_getParam('qcm', null);

		if($this->getRequest()->isPost()){
			if($form->isValid($request->getPost())){
				$values = $form->getValues();
				$db = new Application_Model_DbTable_Qcm();
				$db->update($values, array('id = ?'=>$values['id']));
				$this->_helper->redirector('view','qcm','prof',array('qcm'=>$values['id']));
			}else{
				$this->view->form = $form;
				$errors = $form->getMessages();
				$messages = array();
				foreach ($errors as $err) {
					foreach($err as $mess){
						$messages[]=$mess;
					}
				}
				$this->view->errors = $messages;
			}
		}else{
			if($id>0){
				$db = new Application_Model_DbTable_Qcm();
				$form->populate($db->find($id)->current()->toArray());
			}
			$this->view->form = $form;
		}
	}
	public function viewAction(){
		$dbQcm = new Application_Model_DbTable_Qcm();
		$dbQuest = new Application_Model_DbTable_Question();
		$id = (int) $this->_getParam('qcm', null);

		if($id>0){
			$qcm = $dbQcm->findWithQuestions($id);
			$this->view->qcm = $qcm;
			$questArray = $qcm->questions;
				
			$page =  (int) $this->_getParam('page', 1);
			$paginator = Zend_Paginator::factory($questArray);
			$paginator	->setItemCountPerPage(10)
			->setCurrentPageNumber($page)
			->setPageRange(3);
			$this->view->entries = $paginator;
				
		}else{
			$this->_helper->redirector('index');
		}
	}
	public function deleteAction(){
		$id = (int) $this->_getParam('qcm', null);
		if($id>0){
			$repDB = new Application_Model_DbTable_Reponse();
			$repDB->delete($repDB->getAdapter()->quoteInto('id_qcm = ?',$id));
			
			$questDB = new Application_Model_DbTable_Question();
			$questDB->delete($questDB->getAdapter()->quoteInto('id_qcm = ?',$id));
			
			$attribDB = new Application_Model_DbTable_Attribution();
			$attribDB->delete($attribDB->getAdapter()->quoteInto('id_qcm = ?',$id));
			
			$demDB = new Application_Model_DbTable_Demand();
			$demDB->delete($demDB->getAdapter()->quoteInto('id_qcm = ?',$id));
			
			$qcmDB = new Application_Model_DbTable_Qcm();
			$qcmDB->delete($qcmDB->getAdapter()->quoteInto('id = ?',$id));
			
			$this->_helper->redirector('index');
		}else{
			$this->_helper->redirector('deny','error');
		}
	}
	public function unlinkAction(){
		$id = (int) $this->_getParam('qcm', null);
		if($id>0){
			$db = new Application_Model_DbTable_Demand();
			$db->delete(array('id_qcm = ?'=>$id));
			
			$this->_helper->redirector('view','qcm','prof',array('qcm'=>$id));
		}else{
			$this->_helper->redirector('deny','error');
		}
	}
}